package servlet.admin;

import com.google.gson.Gson;
import entity.Apply;

import service.ApplyService;
import service.Impl.ApplyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(urlPatterns = "/admin/ApplyEdit")
public class ApplyEditServlet extends HttpServlet {
    private ApplyService applyService = new ApplyServiceImpl();

//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        int id = Integer.parseInt(req.getParameter("id"));
//        String state = req.getParameter("state");
//        Apply apply = new Apply();
//        apply.setId(id);
//        apply.setState(Integer.parseInt(state));
//        applyService.update(apply);
//        resp.sendRedirect(req.getContextPath()+"/admin/ApplyList");
//    }
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("application/json;charset=UTF-8");
    resp.setCharacterEncoding("UTF-8");
    int id = Integer.parseInt(req.getParameter("id"));
    Apply apply = applyService.findById(id);

    // 设置响应内容类型为JSON
    resp.setContentType("application/json");
    PrintWriter out = resp.getWriter();
    out.print(new Gson().toJson(apply));
    out.flush();
}

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        int state = Integer.parseInt(req.getParameter("state"));
        Apply apply = new Apply();
        apply.setId(id);
        apply.setState(state);
        applyService.update(apply);
        resp.sendRedirect(req.getContextPath() + "/admin/ApplyList");
    }

}


